package com.wxisme.ssm.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wxisme.ssm.po.Album;
import com.wxisme.ssm.po.Photo;
import com.wxisme.ssm.po.UserInfo;
import com.wxisme.ssm.service.AlbumService;
import com.wxisme.ssm.service.PhotoService;
import com.wxisme.ssm.service.UserInfoService;

/**
 *
 *@author wxisme
 *@time 2015-9-18 下午12:29:23
 */
@Controller
public class AlbumController {
	
	
	@Autowired
	private AlbumService albumService;
	
	@Autowired
	private PhotoService photoService;
	
	@Autowired
	private UserInfoService userInfoService;
	
	/**
	 * 创建相册controller
	 * @param session
	 * @param request
	 * @param response
	 * @param albumName 相册名
	 * @param description 相册描述
	 * @param isPublic 是否公开
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/createAlbum.action")
	public ModelAndView createAlbum(HttpSession session, HttpServletRequest request, HttpServletResponse response, 
			String albumName, String description, String isPublic) throws Exception {
		//参数可以用request获取
		
		ModelAndView modelAndView = new ModelAndView();
		
		Album album = new Album();
		album.setAlbumName(albumName);
		album.setDescription(description);
		album.setIsPublic(isPublic.equals("true") ? 1:0);
		album.setPhotoNumber(0);
		album.setUsername((String) session.getAttribute("username"));
		
		//设置创建时间
		Date date = new Date();
		long createTime = date.getTime();
		album.setCreateTime(createTime);
		
		//设置唯一路径uuid作为相册的文件名
		String uuid = UUID.randomUUID().toString();
		album.setPathName(uuid);
		
		//将相册信息插入数据库
		
		albumService.insertAlbum(album);
		
		//创建相册对应的文件夹
		
		String albumPath = request.getServletContext().getRealPath("/") + "imgdata/"
				+ session.getAttribute("username") + "file/" + uuid;
		
		File albumFile = new File(albumPath);
		
		albumFile.mkdir();
		
		
		//向用户session中添加新建相册信息
		Map<Album, List<Photo>> albumList = (Map<Album, List<Photo>>) session.getAttribute("userLoadingMap");
		
		albumList.put(album, new ArrayList<Photo>());
		
		//更新UserInfo数据中的albumNumber
		
		UserInfo userInfo = new UserInfo();
		userInfo.setUsername((String) session.getAttribute("username"));
		userInfo.setAlbumNumber(albumList.size());
		userInfoService.updateAlbumNumber(userInfo);
		
		
		
		
		
		modelAndView.setViewName("personal");
		
		
		return modelAndView;
	}
	
	/**
	 * 删除相册controller
	 * @param request
	 * @param session
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/deleteAlbum.action")
	public ModelAndView deleteAlbums(HttpServletRequest request, HttpSession session) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		
		String[] delAlbumsPath = request.getParameterValues("deletingAlbum");
		Map<Album, List<Photo>> userLoadingMap = (Map<Album, List<Photo>>) session.getAttribute("userLoadingMap");
		
		for(int i=0; i<delAlbumsPath.length; i++) {
			//删除对应的相册
			albumService.deleteAlbum(delAlbumsPath[i]);
			//删除相册对应的多个照片
			photoService.deletePhotoByAlbum(delAlbumsPath[i]);
			//移除session中缓存的相册相关信息
			//这样效率太低了，后期需要优化
			for(Album album : userLoadingMap.keySet()) {
				if(album.getPathName().equals(delAlbumsPath[i])) {
					userLoadingMap.remove(album);
					break;
				}
			}
			
		}
		
		
		modelAndView.setViewName("personal");
		
		return modelAndView;
	}

}
